<?php
/**
* This is part of appEngine, a framework kit embedded in webappkit
*
* @link http://webappkit.net
* @package appEngine
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* http request definition rule for webappkit/appengine framework
* from a rule, applies or not to http request
* @uses httpRequestBean
* @todo better definition for uri, mapping segments to param ?
*/
class wakRequestDef extends dataBean {
/*--------------------------------------------------------------------
CONSTRUCTOR
--------------------------------------------------------------------*/
	/**
	* constructor alias
	* @access public
	*/
	function wakRequestDef($def) {
		$this->__construct($def);
	}/**/

	/**
	* "real" constructor
	* @param string $def request definition {method}:{uri-regex}
	* @param array $calls {target var datapath} => {event call}
	* @access public
	*/
	function __construct($def) {
		// parsing request definition
		$def=explode(':',$def);
		if ($def[0])
			$this->set('method',$def[0]);
		if ($def[1])
			$this->set('uri',$def[1]);
		// importing calls
		//$this->set('calls',$calls);
	}/**/
/*--------------------------------------------------------------------
PUBLIC STATIC METHODS
--------------------------------------------------------------------*/
	/**
	*
	* @static
	* @param
	* @return
	* /
	function () {

	}/**/
/*--------------------------------------------------------------------
PUBLIC INSTANCE METHODS
--------------------------------------------------------------------*/
	/**
	* checks if route applies to http request
	* @param httpRequestBean $req
	* @access public
	* @return bool
	*/
	function appliesToRequest($req) {
		if (!is_a($req,'httprequestbean')) {
			trigger_error("wakRequestDef->appliesToRequest(\$req) : \$req have to be an httpRequestBean instance");
			return false;
		}
		// checking http method
		if ($this->method!='*'
		and $this->method!=$req->get('method')) {
			// invalid method
			// print_r($req);
			return false;
		}
		// checking uri regex
		if (!is_null($this->uri)
		and !ereg($this->uri,'/'.$req->getUri())) {
			return false;
		}
		return true;
	}/**/

	/**
	* get event calls
	* @access public
	* @return array associative, target var => event-call
	* /
	function getCalls() {
		return $this->get('calls');
	}/**/

	/**
	*
	* @param
	* @access public
	* @return
	* /
	function () {

	}/**/
/*--------------------------------------------------------------------
VARS / MEMBERS
--------------------------------------------------------------------*/
	/**
	* @var string http method : GET|POST|null
	*/
	var $method='*';
	function _method_inputfilter($x) {
		if (is_string($x))
			$x=strtoupper($x);
		else
			$x=null;
		return $x;
	}
	function _method_check($x) {
		return in_array($x,array('*','GET','POST','PUT','DELETE'));
	}

	/**
	* @var string uri regular expression check for URIs
	* uris have to include leading /
	*/
	var $uri='^/$';
	function _uri_check($x) {
		return is_string($x);
	}
	function _uri_inputfilter($x) {
		// joker
		if ($x=='*')
			return '^.*$';
		// adding regex starting and ending delimiters if needed
		if (substr($x,0,1)!='^')
			$x='^'.$x;
		if (substr($x,-1,1)!='$')
			$x=$x.'$';
		return $x;
	}

/*--------------------------------------------------------------------
PRIVATE / PROTECTED METHODS
--------------------------------------------------------------------*/
	/**
	*
	* @param
	* @access protected
	* @return
	* /
	function () {

	}/**/
}
?>